#!/usr/bin/python3
# Filename : UpdateFuture.py
#Author:Bill Zhang
#Email:zchv@msn.com
#Future collection thread

#import httplib2
import httplib2
#import the BeautifulSoup which parse the html
from bs4 import BeautifulSoup
from bs4.diagnose import diagnose
#import python's reguler expression lib
import re

def getData(URL):
	#URL='http://www.digikey.cn/product-detail/zh/BFC280800006/BC2767-ND/3028995'
	h=httplib2.Http()
	headers={'Connection': 'keep-alive','Content-type': 'application/x-www-form-urlencoded','User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6','Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8','Accept-Language':'zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4'}
	#HtmlCode save the source code of the webpages
	resp,content=h.request(URL,'GET',headers=headers)
	if content!=0:
	    print("having got the html source")
	if resp.status == 200 :
		content=content.decode('utf-8')
		#content=content.decode('utf-8')
	#print(content)
	##parse the html by using the BeautifulSoup

	#f = open(URL[-7:], 'w') # open for 'w'riting
	#f.write(HtmlCode) # write text to file
	#f.close() # close the file

	#re extract content Future pattern
	Future=BeautifulSoup(content,["lxml", "xml"])

	#get product's quantity 
	quantity=(Future.find("td", { "class" : "qty" })).text
	ProductQuantity=int(quantity)
	#print(quantity)

	#ProductQuantity=re.search(r"\d*,\d*",quantity).group(0).replace(',','')
	# print(ProductQuantity)

	#get product's price
	price=Future.find_all("tr", { "class" : "price-break" })
	
	price=str(price)
	#print(price)

	ProductPrice=re.findall(r"(?x)[>$](\d+\.?\d*)",price)
	#print(ProductPrice)
	
	ProductPrice.insert(0,ProductQuantity)
	ProductPrice = map(float, ProductPrice)
	return ProductPrice
	#[库存，数量级，价格，数量级，价格......]
	#[350.0, 70.0, 0.429, 140.0, 0.396, 700.0, 0.363, 1540.0, 0.33]
